package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.ForumReply;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.ForumReplyService;
import com.czwj.util.TextUtil;

/**
* @ClassName: ForumReplyAction
* @Description: TODO
* @author H
* @date 2020年6月1日
*
*/
@Controller
public class ForumReplyAction{
	@Resource
	ForumReplyService forumReplyService;
	
	@RequestMapping(value = "/forumReplys",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ForumReply> forumReply(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "postid",required = false) String postid,
			@RequestParam(value = "createrid",required = false) String createrid){
		DataGridData<ForumReply> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(postid)){
			map.put("postid",postid);
		}
		if(TextUtil.isNotEmpty(createrid)){
			map.put("createrid",createrid);
		}
		List<ForumReply> list = forumReplyService.search(map);
		result.setRows(list);
		result.setTotal(forumReplyService.count(map));
		return result;
	}
	
	@SystemControllerLog(description = "发帖")
	@RequestMapping(value = "/forumReplySave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult forumReplySave(@RequestParam(value = "images",required = false) MultipartFile[] images,
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "postid",required = false) Integer postid,
			@RequestParam(value = "content",required = false) String content,
			@RequestParam(value = "parentid",required = false) Integer parentid,HttpServletRequest request){
		OperateResult result = new OperateResult();
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		int createrid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		String createip = request.getRemoteAddr();
		count = forumReplyService.insertorupdate(id,postid,content,parentid,createrid,createip,images);
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除物资")
	@RequestMapping(value = "forumReplyDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult forumReplyDelete(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = forumReplyService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
}
